package com.ruoyi.user.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.user.domain.CarType;
import com.ruoyi.user.service.CarBrandService;
import com.ruoyi.user.service.CarTypeService;
import com.ruoyi.user.service.ICarUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.user.domain.CarCarinfo;
import com.ruoyi.user.service.ICarCarinfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户车辆Controller
 * 
 * @author ruoyi
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/carinfo")
@CrossOrigin
public class CarCarinfoController extends BaseController
{
    @Autowired
    private ICarCarinfoService carCarinfoService;

    @Autowired
    private ICarUserService carUserService;

    @Autowired
    private CarTypeService carTypeService;

    @Autowired
    private CarBrandService carBrandService;
    /**
     * 查询用户车辆列表
     */
    @RequiresPermissions("user:carinfo:list")
    @GetMapping("/list")
    public TableDataInfo list(CarCarinfo carCarinfo)
    {
        startPage();
        List<CarCarinfo> list = carCarinfoService.selectCarCarinfoList(carCarinfo);
        //根据用户id一对一查询用户信息
        for (CarCarinfo car : list) {
            car.setCarUser(carUserService.selectCarUserById(car.getUid()));
            car.setCarType(carTypeService.getCarTypeById(car.getTypeId()));
            car.setCarBrand(carBrandService.getCarBrandById(car.getCarType().getBid()));
        }
        return getDataTable(list);
    }

    /**
     * 导出用户车辆列表
     */
    @RequiresPermissions("user:carinfo:export")
    @Log(title = "用户车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarCarinfo carCarinfo)
    {
        List<CarCarinfo> list = carCarinfoService.selectCarCarinfoList(carCarinfo);
        ExcelUtil<CarCarinfo> util = new ExcelUtil<CarCarinfo>(CarCarinfo.class);
        util.exportExcel(response, list, "用户车辆数据");
    }

    /**
     * 获取用户车辆详细信息
     */
    @RequiresPermissions("user:carinfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carCarinfoService.selectCarCarinfoById(id));
    }

    /**
     * 新增用户车辆
     */
    @RequiresPermissions("user:carinfo:add")
    @Log(title = "用户车辆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarCarinfo carCarinfo)
    {
        return toAjax(carCarinfoService.insertCarCarinfo(carCarinfo));
    }

    /**
     * 修改用户车辆
     */
    @RequiresPermissions("user:carinfo:edit")
    @Log(title = "用户车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarCarinfo carCarinfo)
    {
        return toAjax(carCarinfoService.updateCarCarinfo(carCarinfo));
    }

    /**
     * 删除用户车辆
     */
    @RequiresPermissions("user:carinfo:remove")
    @Log(title = "用户车辆", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carCarinfoService.deleteCarCarinfoByIds(ids));
    }

    //测试根据id获取车型信息的接口是否可以正常工作
    @GetMapping("/carType/{id}")
    public AjaxResult getCarTypeById(@PathVariable Long id) {
        CarType carType = carTypeService.getCarTypeById(id);
        return AjaxResult.success(carType);
    }

    //测试根据id获取车辆品牌的接口是否可以正常工作
    @GetMapping("/carBrand/{id}")
    public AjaxResult getCarBrandById(@PathVariable Long id) {
        return AjaxResult.success(carBrandService.getCarBrandById(id));
    }

    @RequiresPermissions("user:carinfo:list")
    @GetMapping("/user/{uid}")
    public AjaxResult getCarsByUser(@PathVariable Long uid) {
        // 1. 创建查询条件对象
        CarCarinfo query = new CarCarinfo();
        query.setUid(uid);

        // 2. 查询该用户的所有车辆基础信息
        List<CarCarinfo> list = carCarinfoService.selectCarCarinfoList(query);

        // 3. 对每辆车补充详细信息
        for (CarCarinfo car : list) {
            // 3.1 查询用户信息
            car.setCarUser(carUserService.selectCarUserById(car.getUid()));

            // 3.2 查询车型信息
            CarType carType = carTypeService.getCarTypeById(car.getTypeId());
            car.setCarType(carType);

            // 3.3 查询品牌信息(通过车型中的品牌ID)
            if (carType != null && carType.getBid() != null) {
                car.setCarBrand(carBrandService.getCarBrandById(carType.getBid()));
            }
        }

        // 4. 返回完整数据
        return AjaxResult.success(list);
    }


}
